<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Models\User;
use App\Http\Models\Goods;
use App\Http\Models\GoodsCorrection;
use App\Http\Models\StatisticsGoodsInfo;
use DB;
use App\Http\Models\UserAuth;
use Auth;
/**
 * 商品纠错控制
 * Creater: lqr
 */
class GoodsCorrectionController extends Controller
{
	/**
	* 提交商品纠错
	* @param Request $request
	* @return string
	*/
	public function addCorrection(Request $request){
		$uid=Auth::id();//获取当前登陆用户ID
		if(empty($uid)){
			return json_encode(['code'=>1,'msg'=>'用户尚未登陆！']);
		}
		$data=$request->only([
			'gid',
			'type',
			'goodstype',
			'content'
			]);
			
		$error = DataValidate($data,[
			'gid'=>'required|numeric',
			'type'=> 'required|numeric',
			'goodstype'=> 'required|numeric',
			'content'=> 'required'
        ]);

        if (count($error) > 0) {
            return json_encode(['code' => 1, 'msg' => $error]);
        }
		
		$gid=GoodsCorrection::insertGetId([
			'gid'=>$data['gid'],
			'userid'=>$uid,
			'state'=>0,
			'type'=>$data['type'],
			'goodstype'=>$data['goodstype'],
			'content'=>$data['content'],
			'created_at'=>date('y-m-d h:i:s',time())
		]);
		if($gid){
			Goods::where(['id' => $data['gid']])->update(['site_error_status' => 1]);
			//更新日志流水
			StatisticsGoodsInfo::updateStatisticsGoods($gid, 3, 0, 0, date('y-m-d h:i:s', time()),-1);
			return json_encode(['code'=>0,'msg'=>'ok']);
		}else{
			return json_encode(['code'=>1,'msg'=>'fail']);
		}		
	}
}
